package com.sanxinsaas.nearstore.framework.util

import android.app.Activity
import android.content.Context
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.TextPaint
import android.text.style.ClickableSpan
import android.view.View
import android.view.inputmethod.InputMethodManager
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.sos.widget.util.clickNoRepeat
import com.sos.widget.view.flyco.tablayout.CommonTabLayout
import com.sos.widget.view.flyco.tablayout.listener.OnTabSelectListener


/**
 * 作者　: hegaojian
 * 时间　: 2020/2/20
 * 描述　:项目中自定义类的拓展函数
 */



//绑定普通的Recyclerview
fun RecyclerView.init(
    layoutManger: RecyclerView.LayoutManager,
    bindAdapter: RecyclerView.Adapter<*>,
    isScroll: Boolean = true,
): RecyclerView {
    layoutManager = layoutManger
    setHasFixedSize(true)
    adapter = bindAdapter
    isNestedScrollingEnabled = isScroll
    return this
}



fun ViewPager2.init(
    act: FragmentActivity,
    fragments: ArrayList<Fragment>,
    isUserInputEnabled: Boolean = true,
): ViewPager2 {
    this.offscreenPageLimit = fragments.size
    //是否可滑动
    this.isUserInputEnabled = isUserInputEnabled
    //设置适配器
    adapter = object : FragmentStateAdapter(act.supportFragmentManager, act.lifecycle) {
        override fun createFragment(position: Int) = fragments[position]
        override fun getItemCount() = fragments.size
    }
    return this
}

fun ViewPager2.init(
    fragment: Fragment,
    fragments: ArrayList<Fragment>,
    isUserInputEnabled: Boolean = true,
): ViewPager2 {
    //是否可滑动
    this.isUserInputEnabled = isUserInputEnabled
    //设置适配器
    adapter = object : FragmentStateAdapter(fragment.childFragmentManager, fragment.lifecycle) {
        override fun createFragment(position: Int) = fragments[position]
        override fun getItemCount() = fragments.size
    }
    return this
}

fun ViewPager2.initMain(fragment: Fragment, fragmentList: MutableList<Fragment>): ViewPager2 {
    //是否可滑动
    this.isUserInputEnabled = false
    this.offscreenPageLimit = fragmentList.size
    //设置适配器
    adapter = object : FragmentStateAdapter(fragment) {
        override fun createFragment(position: Int): Fragment {
            return fragmentList.get(position);
        }

        override fun getItemCount() = fragmentList.size
    }
    return this
}


/**
 * 隐藏软键盘
 */
fun Activity.hideSoftKeyboard() {
    this.let { act ->
        val view = act.currentFocus
        view?.let {
            val inputMethodManager =
                act.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
            inputMethodManager.hideSoftInputFromWindow(
                view.windowToken,
                InputMethodManager.HIDE_NOT_ALWAYS
            )
        }
    }
}


fun Context.getDrawableByName(name: String): Int? {
    try {
        return resources.getIdentifier(name, "drawable", packageName)
    } catch (e: Exception) {
        e.printStackTrace()
    }
    return null

}

fun Context.getMipmapByName(name: String): Int? {
    try {
        return resources.getIdentifier(name, "mipmap", packageName)
    } catch (e: Exception) {
        e.printStackTrace()
    }
    return null
}

fun Context.getString(id: Int): String {
    return this.resources.getString(id)
}


/**
 * @param textColor 文字颜色
 * @param enableLine 下划线
 * @param enableBold 粗体
 * */
fun SpannableStringBuilder.setClickSpan(
    starIndex: Int,
    length: Int,
    textColor: Int? = null,
    enableLine: Boolean? = null,
    enableBold: Boolean? = null,
    clickCallBack: (() -> Unit?)? = null,
) {

    val clickSpan = object : ClickableSpan() {
        override fun onClick(widget: View) {
            clickCallBack?.invoke()
        }

        override fun updateDrawState(ds: TextPaint) {
            super.updateDrawState(ds)
            textColor?.let {
                ds.setColor(it)
            }
            enableLine?.let {
                ds.isUnderlineText = it
            }
            enableBold?.let {
                ds.isFakeBoldText = it
            }

        }

    }
    this.setSpan(clickSpan, starIndex, starIndex + length, Spannable.SPAN_INCLUSIVE_EXCLUSIVE)
}


/**
 *取消recyclerview动画
 * */
fun RecyclerView.cancelAnimator() {
    /*处理闪烁问题*/
    getItemAnimator()?.setAddDuration(0)
    getItemAnimator()?.setChangeDuration(0)
    getItemAnimator()?.setMoveDuration(0)
    getItemAnimator()?.setRemoveDuration(0)
    setItemAnimator(null)
}

fun View.click(action: (view: View) -> Unit) {
    this.clickNoRepeat(500,action)
}

/**
 * CommonTabLayout+ViewPager2
 * */
fun CommonTabLayout.linkViewPage2(vp: ViewPager2, itemClickBack: ((Int) -> Unit?)? = null) {
    val tab = this
    this.setOnTabSelectListener(object : OnTabSelectListener {
        override fun onTabSelect(position: Int) {
            vp.setCurrentItem(position)
        }

        override fun onTabReselect(position: Int) {
        }

    })
    vp.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
        override fun onPageSelected(position: Int) {
            super.onPageSelected(position)
            tab.setCurrentTab(position)
        }
    })
}


